package edu.gatech.mobile.scene.provider.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import edu.gatech.mobile.scene.model.LabelInfo;


/**
 * Helpers for accessing data in LabelInfo database table 
 * @author jing
 *
 */


public class LabelInfoDBImpl {
	public Context mContext;
	
	public LabelInfoDBImpl(Context context){
		mContext=context;
	}
	
	/**
	 * Save the label into the database.
	 * @param labelInfo
	 */
	public void putItem(LabelInfo labelInfo){
		ContentResolver resolver = mContext.getContentResolver();
		resolver.insert(LabelInfoTable.CONTENT_URI, getQueryValues(labelInfo));
	}
	
	/**
	 * Get a cursor that contains all of the labels
	 * @return the cursor.
	 */
	public Cursor getAllLabelsCursor(){
		ContentResolver resolver = mContext.getContentResolver();
		Cursor cursor =resolver.query(LabelInfoTable.CONTENT_URI, null, null, null, null);
		return cursor;
	}
	
	/**
	 * Retrieve all of the labels in the database.
	 * @return a list of all of the labels in the database.
	 */
	public List<LabelInfo>getLabels(){
		List<LabelInfo> items = new ArrayList<LabelInfo>();
		ContentResolver resolver = mContext.getContentResolver();
		Cursor cursor =resolver.query(LabelInfoTable.CONTENT_URI, null, null, null, null);
		while (cursor != null && cursor.getCount() > 0 && !cursor.isLast()){
			LabelInfo item =  fillFromCursor(cursor);
			items.add(item);
		}
		safelyCloseCursor(cursor);
		return items;
	}
	
	/**
	 * Get a label with a given label name
	 * @param labelName label name of the label 
	 * @return the label with the given name
	 */
	public LabelInfo getLabelByName(String labelName){
		ContentResolver resolver = mContext.getContentResolver();
		String where = LabelInfoTable.LABEL_NAME + "= '"+ labelName.toLowerCase()+"'";
		Cursor cursor =resolver.query(LabelInfoTable.CONTENT_URI, null, where, null, null);
		LabelInfo label = null;
		if (cursor != null && cursor.getCount() > 0 && !cursor.isLast()){
			label = fillFromCursor(cursor);
		}
		safelyCloseCursor(cursor);
		return label;
	}
	
	/**
	 * Construct a table of values to put into the database
	 * @param labelInfo label to be put into the table
	 * @return table with labelInfo values.
	 */
	public ContentValues getQueryValues(LabelInfo labelInfo){
		long time;
		if(labelInfo.getTime() == null){
			time = System.currentTimeMillis();
		}
		else{
			time = labelInfo.getTime();
		}
		ContentValues values = new ContentValues();
		values.put(LabelInfoTable.LABEL_NAME, labelInfo.getLabelName());
		values.put(LabelInfoTable.COLOR, labelInfo.getColor());
		values.put(LabelInfoTable.TIME_POSTED, time);
		return values;
	}
	
	/**
	 * Construct a LabelInfo object with the values in cursor
	 * @param cursor cursor containing LabelInfo values.
	 * @return LableInfo with values in the cursor.
	 */
	public LabelInfo fillFromCursor(Cursor cursor){
		cursor.moveToNext();
		LabelInfo labelInfo = new LabelInfo();
		labelInfo.setLabelName(cursor.getString(cursor.getColumnIndex(LabelInfoTable.LABEL_NAME)));
		labelInfo.setColor(cursor.getInt(cursor.getColumnIndex(LabelInfoTable.COLOR)));
		labelInfo.setTime(cursor.getLong(cursor.getColumnIndex(LabelInfoTable.TIME_POSTED)));
		labelInfo.setId(cursor.getInt(cursor.getColumnIndex(LabelInfoTable._ID)));
		return labelInfo;
	}
	
	private void safelyCloseCursor(Cursor cur) {
		if(cur != null && cur.isClosed() == false)
			cur.close();
	}
	
}
